package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.context.BaseContext;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;

import java.util.HashMap;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录:{}" + employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);
        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * 查询员工页
     *
     * @return {@link Result}<{@link ?}>
     */
    @GetMapping("/page")
    public Result<PageResult> queryEmployeePage(String name, Integer page, Integer pageSize) {
        PageResult pageResult = employeeService.queryEmployeePage(name, page, pageSize);
        return Result.success(pageResult);
    }

    /**
     * 更新员工状态
     *
     * @param id     id
     * @param status 状态
     * @return {@link Result}<{@link ?}>
     */
    @PostMapping("/status/{status}")
    public Result<?> updateEmployeeStatus(Integer id, @PathVariable Integer status) {
        employeeService.updateEmployeeStatus(id,status);
        return Result.success();
    }

    /**
     * 按id查询员工
     *
     * @param id id
     * @return {@link Result}<{@link Employee}>
     */
    @GetMapping("{id}")
    public Result<Employee> queryEmployeeById(@PathVariable Long id){
        Employee employee = employeeService.queryEmployeeById(id);
        return Result.success(employee);
    }

    /**
     * 更新员工
     *
     * @param employeeDTO 员工dto
     * @return {@link Result}<{@link ?}>
     */
    @PutMapping
    public Result<?> updateEmployee(@RequestBody EmployeeDTO employeeDTO){
        System.out.println("employeeDTO = " + employeeDTO);
        employeeService.updateEmployee(employeeDTO);
        return Result.success();
    }

    /**
     * 保存员工
     *
     * @param employeeDTO 员工dto
     * @return {@link Result}<{@link ?}>
     */
    @PostMapping()
    public Result<?> saveEmployee(@RequestBody EmployeeDTO employeeDTO){
        employeeService.saveEmployee(employeeDTO);
        return Result.success();
    }
}
